package com.sf.fy.uav.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.enums.ActivityProductTypeEnum;
import com.sf.fy.uav.common.request.operate.SysPromotionRecordsRequest;
import com.sf.fy.uav.common.response.operational.SysPromotionRecordsResponse;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.PhoneEncoderUtil;
import com.sf.fy.uav.entity.PromotionRecords;
import com.sf.fy.uav.mapper.PromotionRecordsMapper;
import com.sf.fy.uav.service.IPromotionRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 推广记录表 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2023-08-18
 */
@Service
public class PromotionRecordsServiceImpl extends ServiceImpl<PromotionRecordsMapper, PromotionRecords> implements IPromotionRecordsService {

    @Autowired
    private PromotionRecordsMapper promotionRecordsMapper;

    @Autowired
    private PhoneEncoderUtil phoneEncoderUtil;

    @Override
    public Long getTotal(String payerPhoneNumber, String promotionPhoneNumber, String tcWaybillNo, String activityId) {
        QueryWrapper<PromotionRecords> promotionRecordsQueryWrapper = this.getQueryWrapper(
                payerPhoneNumber, promotionPhoneNumber, tcWaybillNo
        );
        promotionRecordsQueryWrapper.eq("activity_id", activityId).and(i -> i.ne("promotion_reward", null).or().ne("promotion_reward", new BigDecimal(0)));
        promotionRecordsQueryWrapper.orderByDesc("finish_time");
        return promotionRecordsMapper.selectCount(promotionRecordsQueryWrapper);
    }

    @Override
    public List<SysPromotionRecordsResponse> getPromotionRecords(SysPromotionRecordsRequest sysPromotionRecordsRequest) {
        Integer page = sysPromotionRecordsRequest.getPage();
        Integer size = sysPromotionRecordsRequest.getSize();
        Page<PromotionRecords> promotionRecordsPage = new Page<>(page, size);
        QueryWrapper<PromotionRecords> promotionRecordsQueryWrapper = this.getQueryWrapper(
                sysPromotionRecordsRequest.getPayerPhoneNumber(), sysPromotionRecordsRequest.getPromotionPhoneNumber(), sysPromotionRecordsRequest.getTcWaybillNo()
        );
        promotionRecordsQueryWrapper.eq("activity_id", sysPromotionRecordsRequest.getActivityId()).and(i -> i.ne("promotion_reward", null).or().ne("promotion_reward", new BigDecimal(0)));
        promotionRecordsQueryWrapper.orderByDesc("finish_time");
        IPage<PromotionRecords> promotionRecordsIPage = promotionRecordsMapper.selectPage(promotionRecordsPage, promotionRecordsQueryWrapper);
        List<SysPromotionRecordsResponse> sysPromotionRecordsResponses = BeanUtil.copyProperties(promotionRecordsIPage.getRecords(), SysPromotionRecordsResponse.class);
        sysPromotionRecordsResponses.forEach(sysPromotionRecordsResponse -> {
            if (sysPromotionRecordsResponse.getPromotionReward() == null) {
                sysPromotionRecordsResponse.setPromotionReward(BigDecimal.ZERO);
            }
            String promotionPhoneNumber = phoneEncoderUtil.decryptString(sysPromotionRecordsResponse.getPromotionPhoneNumber());
            String payerPhoneNumber = phoneEncoderUtil.decryptString(sysPromotionRecordsResponse.getPayerPhoneNumber());
            sysPromotionRecordsResponse.setPromotionPhoneNumber(promotionPhoneNumber.substring(0, 3) + "****" + promotionPhoneNumber.substring(7));
            sysPromotionRecordsResponse.setPayerPhoneNumber(payerPhoneNumber.substring(0, 3) + "****" + payerPhoneNumber.substring(7));
            if (ObjectUtil.isNotNull(sysPromotionRecordsResponse.getRewardType())) {
                sysPromotionRecordsResponse.setRewardTypeStr(ActivityProductTypeEnum.getName(sysPromotionRecordsResponse.getRewardType()));
            }
        });
        return sysPromotionRecordsResponses;
    }

    private QueryWrapper getQueryWrapper(String payerPhoneNumber, String promotionPhoneNumber, String tcWaybillNo) {
        QueryWrapper<PromotionRecords> promotionRecordsQueryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(payerPhoneNumber)) {
            promotionRecordsQueryWrapper.eq("payer_phone_number", phoneEncoderUtil.encryptString(payerPhoneNumber));
        }
        if (!ObjectUtils.isEmpty(promotionPhoneNumber)) {
            promotionRecordsQueryWrapper.eq("promotion_phone_number", phoneEncoderUtil.encryptString(promotionPhoneNumber));
        }
        if (!ObjectUtils.isEmpty(tcWaybillNo)) {
            promotionRecordsQueryWrapper.like("tc_waybill_no", tcWaybillNo);
        }
        return promotionRecordsQueryWrapper;
    }
}
